package com.smartpower.wattmeter.ui;

import android.view.KeyEvent;
import android.view.View;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.android.basiclib.base.BaseActivity;
import com.android.basiclib.uitls.NetWorkUtil;
import com.android.basiclib.uitls.ToastUtils;
import com.clj.fastble.BleManager;
import com.smartpower.wattmeter.R;

public class MainActivity extends BaseActivity implements View.OnClickListener {

    private FragmentTransaction ft;
    private HomeScanFragment mHomeScanFragment;
    private HomeOrderFragment mHomeOrderFragment;
    private int mCurrentPage = 0;
    private android.widget.LinearLayout mLlGroupScan;
    private android.widget.LinearLayout mLlGroupOrder;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_main;
    }

    @Override
    protected void init() {
        initView();
        initListener();
    }

    private void initListener() {
        //默认选中第0个
        selectorPage(0);
        setBottomSelected(mLlGroupScan);

        mLlGroupScan.setOnClickListener(this);
        mLlGroupOrder.setOnClickListener(this);
    }

    @Override
    public void onNetworkConnectionChanged(boolean isConnected, NetWorkUtil.NetworkType networkType) {
    }

    @Override
    protected void dataObserver() {
    }

    /**
     * 选中的当前Fragment
     */
    private void selectorPage(int position) {
        FragmentManager fm = getSupportFragmentManager();
        ft = fm.beginTransaction();
        hideFragment();
        switch (position) {
            case 0:
                if (mHomeScanFragment == null) {
                    mHomeScanFragment = new HomeScanFragment();
                    ft.add(R.id.main_fragment_container, mHomeScanFragment);
                }
                ft.show(mHomeScanFragment);
                mCurrentPage = 0;
                break;
            case 1:
                if (mHomeOrderFragment == null) {
                    mHomeOrderFragment = new HomeOrderFragment();
                    ft.add(R.id.main_fragment_container, mHomeOrderFragment);
                }
                ft.show(mHomeOrderFragment);
                mCurrentPage = 1;
                break;
        }
        //提交事务
        ft.commitAllowingStateLoss();
    }

    /**
     * 展示当前的隐藏其他的Fragment
     */
    private void hideFragment() {
        if (mHomeScanFragment != null) {
            ft.hide(mHomeScanFragment);
        }
        if (mHomeOrderFragment != null) {
            ft.hide(mHomeOrderFragment);
        }
    }

    /**
     * 设置选中效果
     */
    private void setBottomSelected(View layout) {
        //先还原，全部都是未选中。
        mLlGroupScan.setSelected(false);
        mLlGroupOrder.setSelected(false);
        //传递进来的Id设置为选中：
        layout.setSelected(true);
    }

    private void initView() {
        mLlGroupScan = findViewById(R.id.ll_group_scan);
        mLlGroupOrder = findViewById(R.id.ll_group_order);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_group_scan:
                selectorPage(0);
                setBottomSelected(mLlGroupScan);
                break;
            case R.id.ll_group_order:
                selectorPage(1);
                setBottomSelected(mLlGroupOrder);
                break;
        }
    }

    //点击返回键返回桌面而不是退出程序
    private long firstTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if (System.currentTimeMillis() - firstTime > 2000) {
                ToastUtils.makeText(mActivity, "再次点击，退出应用");
                firstTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        BleManager.getInstance().disconnectAllDevice();
    }
}
